//
//  PopUpView.m
//  MyNewWeiBo
//
//  Created by wpapple on 16/5/31.
//  Copyright © 2016年 heron. All rights reserved.
//

#import "PopUpView.h"

static MenuBlock _block;

static UIButton * _coverBtn;



@implementation PopUpView

+ (void)popUpViewWithContentView:(UIView *)contentView clickView:(UIView *)clickView withTarget:(id)target display:(MenuBlock)block{
    
    _block = [block copy];
    
    UIWindow * popUpWindow = [UIApplication sharedApplication].keyWindow;
    
    UIButton * coverBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    coverBtn.frame = popUpWindow.frame;
    [popUpWindow addSubview:coverBtn];
    
    _coverBtn = coverBtn;
    
    [_coverBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIImageView * imageVeiw = [[UIImageView alloc]init];
    imageVeiw.image = [UIImage imageNamed:@"popover_background"];
    
    contentView.width = 197;
    
    imageVeiw.width = contentView.width + 20;
    imageVeiw.height = contentView.height + 25;
    CGPoint centerPoint = [popUpWindow convertPoint:clickView.center fromView:clickView.superview];
    imageVeiw.center = centerPoint;
    CGRect senderRect = [popUpWindow convertRect:clickView.frame fromView:clickView.superview];
    imageVeiw.y = CGRectGetMaxY(senderRect);
    [coverBtn addSubview:imageVeiw];
    
    contentView.x = 10;
    contentView.y = 15;
    [imageVeiw addSubview:contentView];
    
}

+ (void)btnClick{
    if (_block != nil) {
        _block();
    }
    [_coverBtn removeFromSuperview];
    
}

@end
